﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.WebControls;
using FrameWork.Components;
using System.Collections;

namespace FrameWork.web.Module.IntegralMS.IntegralMS.ClassManager
{
    public partial class New : System.Web.UI.Page
    {
        int ClassID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ClassID = (int)Common.sink("ClassID", MethodType.Get, 255, 1, DataType.Int);
            BindButton();
            if (!Page.IsPostBack)
            {
                OnStart();
                BingdListBox();
            }
        }
        private void BindButton()
        {
            HeadMenuButtonItem bi0 = new HeadMenuButtonItem();
            bi0.ButtonIcon = "back.gif";
            bi0.ButtonName = "返回";
            bi0.ButtonPopedom = PopedomType.List;
            bi0.ButtonUrl = string.Format("GroupList.aspx?ClassID={0}", ClassID);
            HeadMenuWebControls1.ButtonList.Add(bi0);

        }
        private void BingdListBox()
        {
            QueryParam qp=new QueryParam();
            int RecordCount;
            ArrayList list = BusinessFacade.sys_UserList(qp, out RecordCount);
            sys_UserTable user = new sys_UserTable();
            user.UserID = 0;
            user.U_CName = "无";
            list.Insert(0, user);
            lboxUserList.DataSource = list;
            lboxUserList.DataTextField = "U_CName";
            lboxUserList.DataValueField = "UserID";
            lboxUserList.DataBind();
            lboxUserList.SelectedIndex = 0;
        }
        private void OnStart()
        {
            if (ClassID == 0)
                C_ParentID_Txt.Text = "阶段列表";
            else
            {
                C_ParentID_Txt.Text = BusinessFacade.stu_ClassInfoDisp(ClassID).C_CName;
            }

            CatListTitle.Text = string.Format("<a href='GroupList.aspx'>阶段列表</a>{0}", BusinessFacade.GetClassInfoTitle(ClassID));
            //Button1.Attributes.Add("Onclick", "javascript:return checkForm(aspnetForm);");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int RecordCount = 0;
            FrameWorkPermission.CheckPermissionVoid(PopedomType.New);
            stu_ClassInfoTable gt = BusinessFacade.stu_ClassInfoDisp(ClassID);
            gt.C_CName = (string)Common.sink(C_CName.UniqueID, MethodType.Post, 50, 1, DataType.Str);
            gt.C_UserIDStr = "";
            stu_ClassTeacherRelationTable s1 = new stu_ClassTeacherRelationTable
            {

                R_ClassID = ClassID,
                R_TeacherID = 0,
                DB_Option_Action_ = "Delete"
            };
            BusinessFacade.stu_ClassTeacherInsertUpdate(s1);
            foreach (ListItem item in lboxUserList.Items)
            {
                if (item.Selected&&item.Value!="0")
                {
                    stu_ClassTeacherRelationTable s = new stu_ClassTeacherRelationTable
                    {
                        R_ClassID = ClassID,
                        R_TeacherID = Convert.ToInt32(item.Value),
                        DB_Option_Action_ = "Insert"
                    };
                    BusinessFacade.stu_ClassTeacherInsertUpdate(s);
                }
            }
            if (gt.ClassID == 0)
            {
                gt.C_Level = 1;
                BusinessFacade.stu_ClassInfoList(new QueryParam(), out RecordCount);
                gt.C_ShowOrder = RecordCount + 1;
            }
            else
            {

                gt.C_Level++;
                gt.C_ShowOrder = gt.C_ChildCount + 1;
                gt.C_ChildCount = 0;
                gt.C_ParentID = ClassID;
            }

            gt.DB_Option_Action_ = "Insert";
            BusinessFacade.stu_ClassInsertUpdate(gt);
            //更新子部门数
            BusinessFacade.Update_Table_Fileds("stu_ClassInfo", "C_ChildCount=C_ChildCount+1", string.Format("ClassID={0}", ClassID));
            EventMessage.MessageBox(1, "操作成功", string.Format("增加班级{0})成功!", gt.C_CName), Icon_Type.OK, Common.GetHomeBaseUrl(string.Format("GroupList.aspx?ClassID={0}", ClassID)), Common.BuildJs);
        }
    }
}